package com.jjc.android.activity

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.toast.ToastUtils
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.adapter.ProductManualAdapter
import com.jjc.android.base.BaseActivity
import com.jjc.android.entity.KeyValuePair
import com.jjc.android.entity.LzyResponse
import com.jjc.android.entity.PageInfo
import com.jjc.android.entity.ProductManualDto
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.MultiLanguageUtil
import com.jjc.android.util.SimpleOnRefreshLoadMoreListener
import com.jjc.android.util.paramEx
import com.jjc.android.view.CommonKt
import com.lzy.okgo.OkGo
import com.scwang.smart.refresh.layout.SmartRefreshLayout

class ProductManualActivity : BaseActivity(){

    override val layoutId: Int = R.layout.activity_operation_video
    private val smartRefreshLayout: SmartRefreshLayout by lazy { findViewById<SmartRefreshLayout>(R.id.smartRefreshLayout) }
    private val recyclerView: RecyclerView by lazy { findViewById<RecyclerView>(R.id.recyclerView) }
    private lateinit var mAdapter:ProductManualAdapter
    private val refreshLayoutDelegate by lazy {
        object : SimpleOnRefreshLoadMoreListener(){
            override fun invokeActivity(block: BaseActivity.() -> Unit) {
                this@ProductManualActivity.apply(block)
            }

            override fun loadPage(
                page: Int,
                actionSuccess: (Int, List<Any>?) -> Unit,
                actionError: (Int, String?) -> Unit
            ) {
                OkGo.post<LzyResponse<PageInfo<ProductManualDto>>>(Constants.getFormList)
                    .paramEx(mutableMapOf(
                        "page" to page.toString(),
                        "pagesize" to Constants.PAGE_SIZE.toString(),
                        "needpage" to "1",
                        "formheaduuid" to "a5dfbf68671c420088f9342b38cb1843",
                        "filters" to listOf<KeyValuePair>(
                            KeyValuePair("nolbzw5xemf0md5e","fd6f87d5-b60c-4474-85d8-d2241227b8c4"),
                            KeyValuePair("5hn0b3bilpmmxm83",MultiLanguageUtil.getInstance().getValue("19cc4581-b379-4566-b2f5-c45dc411d640","2bc875cb-4a14-4cc0-8c56-b22103a60298"))
                        )
                    ),true)
                    .tag(this)
                    .execute(object : SimpleHttpCallback<LzyResponse<PageInfo<ProductManualDto>>>(false){
                        override fun onCallSuccess(response: LzyResponse<PageInfo<ProductManualDto>>) {
                            actionSuccess.invoke(page,response.data?.data)
                        }

                        override fun onCallError(error: String?) {
                            super.onCallError(error)
                            actionError.invoke(page,error)
                        }
                    })
            }
        }
    }
    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.operate_book_txt))
        recyclerView.layoutManager = LinearLayoutManager(this)
        mAdapter = ProductManualAdapter()
        mAdapter.setOnItemClickListener { _, _, position ->
            val info = mAdapter.getItem(position)?:return@setOnItemClickListener
            info.vwggleltwbqhstqs?.let {
                if(!it.isNullOrEmpty()) it[0] else null
            }?.let {
                PdfViewActivity.start(this,it.url,info.lgjunpn0lx1yl4wv)
            }
        }
        mAdapter.setOnItemChildClickListener { _, view, position ->
            mAdapter.getItem(position)?.vwggleltwbqhstqs?.let {
                if(!it.isNullOrEmpty()) it[0] else null
            }.let {
                if(it==null||it.url.isNullOrEmpty()){
                    ToastUtils.show(getString(R.string.down_url_error_txt))
                }else{
                    CommonKt.downloadFile(this,it.url,true,{
                        ToastUtils.show("${getString(R.string.down_fail_txt)}:$it")
                    }){
                        ToastUtils.show("${getString(R.string.down_success_txt)}:${it.absolutePath}")
                    }
                }

            }
        }
        recyclerView.adapter = mAdapter
        refreshLayoutDelegate.init(smartRefreshLayout,recyclerView,false)
    }
}